/**
 * Depends:
 * utility.json.loader.js
 * utility.json.render.js
 * utility.removeText.js
 */
$.fn.widgetSearchSimple = function(options){
	var $this = this;
	options = options || {};
        
    var categorySwitcherSelector = options.categorySwitcherSelector || 'ul.select-category';
    var categoryCollectionSelector = options.categoryCollectionSelector || 'a';
    var categoryActiveSelector = options.categoryActiveSelector || '.active';
    var categorySelector = options.categorySelector || '[name="category"]'
	var markCollectionSelector  = options.markCollectionSelector  || '[class^="mark-set-"] [id^="markmodel-"]';
	var modelCollectionSelector = options.modelCollectionSelector || '[class^="model-set-"] [id^="markmodel-"]';
	var stateCollectionSelector  = options.stateCollectionSelector  || '[class^="state-set-"] [id^="statecity-"]';
	var cityCollectionSelector   = options.cityCollectionSelector   || '[class^="city-set-"] [id^="statecity-"]';
    var submitSelector = options.submitSelector || 'input.submit';
	/**
	 * 
	 */
	$this._init = function(){
        $this.category        =  $(this).find(categorySelector);
        $this.categorySwitcher = $(this).find(categorySwitcherSelector);
        $this.categoryCollection = $this.categorySwitcher.find(categoryCollectionSelector);
        $this.categoryActive     = $this.categoryCollection.find(categoryActiveSelector);
		$this.markCollection  = $(this).find(markCollectionSelector);
		$this.modelCollection = $(this).find(modelCollectionSelector);
		$this.stateCollection = $(this).find(stateCollectionSelector);
		$this.cityCollection  = $(this).find(cityCollectionSelector);
        $this.submit = $(this).find(submitSelector);
	};
	/**
	 * Binder
	 */
	$this._bindEvents= function(){
		$this.markCollection.change(function(){
			$this.loadModels($(this).attr('name'));
			$this.modelCollection.change();
		});
		
		$this.stateCollection.change(function(){
			$this.loadCities($(this).attr('name'));
			$this.cityCollection.change();
		});
                
                $this.categoryCollection.click(function(){
                    if(!$(this).is(categoryActiveSelector)){
                        $this.category.val($(this).attr('rel'));
                        $this.categoryCollection.removeClass(categoryActiveSelector.replaceAll('.',''));
                        $(this).addClass(categoryActiveSelector.replaceAll('.',''));
                        $this.loadMarks($this.category.val());
                        $this.markCollection.change();
                        
                    };
                    return false;
                });
	};
	
        /**
	 * @todo Make subcategory loading
	 */
	$this.loadMarks = function(category, pairNumber){
                $this.setState($this.markCollection, 'disabled');
                category = category || -1;
                pairNumber = pairNumber || 0;
		var items = utility.json.render({
			data : utility.json.loader({
				url : 'ajax/get-marks',
				data : {category : category}
			})
		});
        $this.setState($this.markCollection, 'enabled');
		$this.markCollection.filter('#markmodel-'+pairNumber+'-mark').empty().append(items);
	};
        
	/**
	 * @todo Make subcategory loading
	 */
	$this.loadModels = function(pairNumber){
        $this.setState($this.modelCollection, 'disabled');
		pairNumber = pairNumber || 0;
		pairNumber = utility.removeText(pairNumber);
		var items = utility.json.render({
			data : utility.json.loader({
				url : 'ajax/get-models',
				data : {identifier : $this.markCollection.filter('#markmodel-'+pairNumber+'-mark').val()}
			})
		});
        $this.setState($this.modelCollection, 'enabled');
		$this.modelCollection.filter('#markmodel-'+pairNumber+'-model').empty().append(items);
	};
	
	/**
	 * 
	 */
	$this.loadCities = function(pairNumber){
                $this.setState($this.cityCollection, 'disabled');
		pairNumber = pairNumber || 0;
		pairNumber = utility.removeText(pairNumber);
		var items = utility.json.render({
			data : utility.json.loader({
				url : 'ajax/get-cities',
				data : {identifier : $this.stateCollection.filter('#statecity-'+pairNumber+'-state').val()}
			})
		});
        $this.setState($this.cityCollection, 'enabled');
		$this.cityCollection.filter('#statecity-'+pairNumber+'-city').empty().append(items);
	};
	
    $this.setState = function(e, state){
        if(state == 'disabled'){
            e.attr('disabled', 'disabled').html('<option value="loading">Загрузка...</option>');
            $this.submit.attr('disabled', 'disabled');
        } else {
            e.removeAttr('disabled');
            $this.submit.removeAttr('disabled');
        }
    };
        
	/**
	 * Implementation
	 */
	$this._init();
	$this._bindEvents();
	
	return $(this);
};